package Test2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-05-13
 * Time: 15:17
 */
public class ReceiverTask implements Runnable{
    private int receivePort;
    public ReceiverTask(int receivePort){
        this.receivePort=receivePort;
    }
    @Override
    public void run() {
        try{
            DatagramSocket ds=new DatagramSocket(receivePort);
            byte[] buf=new  byte[1024];
            DatagramPacket dp=new DatagramPacket(buf,buf.length);
            while(true){
                ds.receive(dp);
                String str=new String(dp.getData(),0,dp.getLength());
                System.out.println("收到"+dp.getAddress().getHostAddress()+"---发来的数据---"+str);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
